﻿using System;
using System.Linq;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Docking;

namespace Infrastructure
{
    public class CustomDockingPanesFactory : DockingPanesFactory
    {
        protected override void AddPane(RadDocking docking, RadPane pane)
        {
            var paneModel = pane as IPaneModel;
            if (paneModel == null || pane is RadDocumentPane)
            {
                base.AddPane(docking, pane);
                return;
            }
            RadPaneGroup paneGroup;
            switch (paneModel.Position)
            {
                case DockState.DockedRight:
                    paneGroup =
                        docking.SplitItems.ToList().FirstOrDefault(i => i.Control.Name == "RightGroup") as
                            RadPaneGroup;
                    paneGroup?.Items.Add(pane);
                    return;
                case DockState.DockedBottom:
                    paneGroup =
                        docking.SplitItems.ToList().FirstOrDefault(i => i.Control.Name == "BottomGroup") as
                            RadPaneGroup;
                    paneGroup?.Items.Add(pane);
                    return;
                case DockState.DockedLeft:
                    paneGroup =
                        docking.SplitItems.ToList().FirstOrDefault(i => i.Control.Name == "LeftGroup") as
                            RadPaneGroup;
                    paneGroup?.Items.Add(pane);
                    return;
                case DockState.FloatingDockable:
                    var splitContainer = docking.GeneratedItemsFactory.CreateSplitContainer();
                    paneGroup = docking.GeneratedItemsFactory.CreatePaneGroup();
                    splitContainer.Items.Add(paneGroup);
                    paneGroup.Items.Add(pane);
                    docking.Items.Add(splitContainer);
                    pane.MakeFloatingDockable();
                    return;
                case DockState.FloatingOnly:
                    var foSplitContainer = docking.GeneratedItemsFactory.CreateSplitContainer();
                    paneGroup = docking.GeneratedItemsFactory.CreatePaneGroup();
                    foSplitContainer.Items.Add(paneGroup);
                    paneGroup.Items.Add(pane);
                    docking.Items.Add(foSplitContainer);
                    pane.MakeFloatingOnly();
                    return;
            }
        }
    }
}